#ifndef ATTENDANCECHECKER_H
#define ATTENDANCECHECKER_H

#include <QObject>
#include <QTime>
#include <QString>
#include <QJsonObject>

class AttendanceRulesService;
class AttendService;

class AttendanceChecker : public QObject
{
    Q_OBJECT
public:
    // 考勤状态枚举（与数据表字段对应）
    enum class CheckStatus {
        NORMAL,      // 正常
        LATE,        // 迟到
        EARLY_LEAVE, // 早退
        ABSENT,      // 缺勤
        NO_RECORD,   // 缺卡
        LEAVE_MORNING, // 上午请假
        LEAVE_AFTERNOON, // 下午请假
        LEAVE_FULL   // 全天请假
    };

    explicit AttendanceChecker(AttendanceRulesService *rulesService,
                               AttendService *attendService,
                               QObject *parent = nullptr);

    // 获取员工适用的考勤规则
    QJsonObject getApplicableRule(const QString &employeeId = "");

    // 判定签到状态
    CheckStatus judgeSignInStatus(const QTime &signInTime, const QJsonObject &rule);

    // 判定签退状态
    CheckStatus judgeSignOutStatus(const QTime &signOutTime, const QJsonObject &rule);

    // 状态枚举转字符串（与数据库字段匹配）
    static QString statusToString(CheckStatus status);

    // 处理打卡记录（新增/更新+状态计算）
    bool processCheckInOut(const QString &employeeId, const QString &currentDateTime);

    // 检查是否为规定时间内打卡
    bool isCheckInWithinAllowedTime(const QString &employeeId, const QDateTime &currentDateTime);

    // 检查是否重复打卡
    bool isDuplicateCheckIn(const QString &employeeId, const QDateTime &currentDateTime, const QString &checkType);

    // 处理请假
    bool processLeave(const QString &employeeId, const QString &recordDate, CheckStatus leaveType);

private:
    AttendanceRulesService *m_rulesService;  // 考勤规则服务
    AttendService *m_attendService;          // 考勤记录服务

    // 验证时间有效性
    bool isValidTime(const QTime &time);

    // 从规则中解析时间
    QTime getTimeFromRule(const QJsonObject &rule, const QString &key);
};

#endif // ATTENDANCECHECKER_H
